home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / t3_1 / doc.lha / documentation / manual / list.mss < prev    next >
Text File  |  1987-06-30  |  15KB  |  457 lines

  1. @part[LIST, root "TMAN.MSS"]    @Comment{-*-System:TMAN-*-}
  2. @chap[Lists]
  3.  
  4.  
  5. @dc{ Say what a list is and why, what an atom is, what a pair is, etc.}
  6.  
  7. @dc{ Talk about various uses of lists:
  8. as sets, as tables, as code trees. }
  9.  
  10. @dc{ Talk about external syntax for lists.  Whitespace, dots, etc. }
  11.  
  12. This section describes routines available for creating, examining,
  13. and otherwise manipulating list structure.
  14.  
  15.  
  16. @section[Predicates]
  17.  
  18. @info[NOTES="Type predicate",EQUIV="NULL"]
  19. @desc[@el[](NULL? @i[object]) @yl[] @i[boolean]]
  20. Returns true if @i[object] is the empty list (null).  Since the empty
  21. list is also used for the logical false value, and non-false objects are
  22. considered to be true, it turns out that @tc[NULL?] is the same as
  23. @tc[NOT].
  24. @EndDesc[NULL?]
  25.  
  26. @AnEquivE[Tfn="PAIR?",Efn="CONSP"]
  27. @info[NOTES="Type predicate",EQUIV="PAIRP"]
  28. @desc[@el[](PAIR? @i[object]) @yl[] @i[boolean]]
  29. Returns true if @i[object] is a pair.
  30.   @begin[ProgramExample]
  31. (PAIR? '(A B C))   @ev[]  @r[true]
  32. (PAIR? '(A . 15))  @ev[]  @r[true]
  33. (PAIR? '())        @ev[]  @r[false]
  34. (PAIR? 'FOO)       @ev[]  @r[false]
  35.   @end[ProgramExample]
  36. @EndDesc[PAIR?]
  37.  
  38. @info[NOTES="Type predicate",EQUIV="ATOM"]
  39. @desc[@el[](ATOM? @i[object]) @yl[] @i[boolean]]
  40. Returns true if @i[object] is an atom.
  41. An atom is any object that isn't a pair.
  42.   @begin[ProgramExample]
  43. (ATOM? @i[object])  @ce[]  (NOT (PAIR? @i[object]))
  44.   @end[ProgramExample]
  45. @EndDesc[ATOM?]
  46.  
  47. @info[NOTES="Type predicate",EQUIV="LISTP"]
  48. @desc[(LIST? @i[object]) @yl[] @i[boolean]]
  49. Returns true if @i[object] is either a pair or the empty list.
  50.   @begin[ProgramExample]
  51. (LIST? '(A B C))   @ev[]  @r[true]
  52. (LIST? '(A . 15))  @ev[]  @r[true]
  53. (LIST? '())        @ev[]  @r[true]
  54. (LIST? 'FOO)       @ev[]  @r[false]
  55.   @end[ProgramExample]
  56. @EndDesc[LIST?]
  57.  
  58. @Comment{ @info[NOTES="Type predicate"] it's not a type predicate. }
  59. @desc[(PROPER-LIST? @i[object]) @yl[] @i[boolean]]
  60. Returns true if @i[list] is a properly formed list, i.e., if its last
  61. pair's cdr is null.
  62. @begin[ProgramExample]
  63. (PROPER-LIST? '(A B C))    @ev[]  @r[true]
  64. (PROPER-LIST? '(A B . C))  @ev[]  @r[false]
  65. (PROPER-LIST? '())         @ev[]  @r[true]
  66. @end[ProgramExample]
  67. @EndDesc[PROPER-LIST?]
  68.  
  69. @desc[(NULL-LIST? @i[list]) @yl[] @i[boolean]]
  70. Like @tc[NULL?], but has an undefined effect (and may, for example,
  71. signal an error) if its argument is not either a pair or null.
  72. @wt[(NULL-LIST? @i[x])] is roughly the same as
  73. @wt[(NULL? (PROCLAIM LIST? @i[x]))].
  74. @EndDesc[NULL-LIST?]
  75.  
  76.  
  77. @section[Constructors]
  78.  
  79. @desc[@el[](CONS @i[object1 object2]) @yl[] @i[pair]]
  80. Returns a new pair whose car is @i[object1] and whose cdr is @i[object2].
  81.   @begin[ProgramExample]
  82. (CONS 'A '())            @ev[]  (A)
  83. (CONS 'A 'B)             @ev[]  (A . B)
  84. (CONS 'A '(B))           @ev[]  (A B)
  85. (CONS 'A (CONS 'B '()))  @ev[]  (A B)
  86. (CONS 'A (LIST 'B 'C))   @ev[]  (A B C)
  87.   @end[ProgramExample]
  88. @EndDesc[CONS]
  89.  
  90. @desc[@el[](LIST . @i[objects]) @yl[] @i[list]]
  91. Returns a new list of its arguments.
  92.   @begin[ProgramExample]
  93. (LIST)                  @ev[]  ()
  94. (LIST 'A)               @ev[]  (A)
  95. (LIST 'A 'B)            @ev[]  (A B)
  96. (LIST 'A (LIST 'B 'C))  @ev[]  (A (B C))
  97. LIST  @ce[]  (LAMBDA THINGS THINGS)
  98.   @end[ProgramExample]
  99. @EndDesc[LIST]
  100.  
  101. @info[EQUIV="LIST*"]
  102. @desc[(CONS* . @i[objects]) @yl[] @i[pair]]
  103. @tc[CONS*] is a generalized @tc[CONS].
  104. It returns a new, possibly improper, list,
  105. by consing the initial @i[objects] onto the last @i[object].
  106.   @begin[ProgramExample]
  107. @tabclear
  108. (CONS* 1 2 3)         @^@ev[]  (1 2 . 3)
  109. (CONS* 'A 'B '(C D))  @ev[]  (A B C D)
  110. (CONS* @i[object])  @\@ce[]  @i[object]
  111. (CONS* @i[object1 object2])@\@ce[]  (CONS @i[object1 object2])
  112. @tabclear
  113.   @end[ProgramExample]
  114. @EndDesc[CONS*]
  115.  
  116. @desc[(COPY-LIST @i[list]) @yl[] @i[list]]
  117. Makes a @qu"top-level" copy of a list; that is, returns a new
  118. list whose elements are the same as the original.
  119.   @begin[ProgramExample]
  120. (COPY-LIST @i[list])  @ce[]
  121.   (COND ((NULL-LIST? @i[list]) '())
  122.         (ELSE (CONS (CAR @i[list]) (COPY-LIST (CDR @i[list])))))
  123.  
  124. (COPY-LIST @i[list])  @ce[]  (MAP IDENTITY @i[list])
  125.   @end[ProgramExample]
  126. @EndDesc[COPY-LIST]
  127.  
  128.  
  129. @section[List access]
  130.  
  131. @info[NOTES="Settable"]
  132. @desc[@el[](CAR @i[pair]) @yl[] @i[object]]
  133. Returns the car of the @i[pair].  By special dispensation,
  134. @wt[(CAR '()) @ev[] ()], even though the empty list is not a pair.
  135. @EndDesc[CAR]
  136.  
  137. @info[NOTES="Settable"]
  138. @desc[@el[](CDR @i[pair]) @yl[] @i[object]]
  139. Returns the cdr of the @i[pair].  By special dispensation,
  140. @wt[(CDR '()) @ev[] ()], even though the empty list is not a pair.
  141. @EndDesc[CDR]
  142.  
  143. @info[NOTES="Settable"]
  144. @desc[(C...R @i[pair]) @yl[] @i[object]]
  145. Compositions of @tc[CAR] and @tc[CDR], up to four deep, are defined.
  146. @begin[ProgramExample]
  147. (CADAR @i[x])   @ce[]  (CAR (CDR (CAR @i[x])))
  148. (CADDDR @i[x])  @ce[]  (CAR (CDR (CDR (CDR @i[x]))))
  149. @end[ProgramExample]
  150. @EndDesc[C...R]
  151.  
  152. @info[NOTES="Settable"]
  153. @desc[@el[](NTH @i[list n]) @yl[] @i[object]]
  154. Returns the @i[n]@+[th] element of @i[list].  The first element (the car) is
  155. @wt[(NTH @i[list] 0)], the second element is @wt[(NTH @i[list] 1)],
  156. and so on.  In general, @i[list] must have at least @i[n]+1 elements.
  157.   @dc{ not the nth! ... }
  158. @begin[ProgramExample]
  159. @tabclear
  160. (NTH '(A B) 0)  @ev[]  A
  161. (NTH '(A B) 1)  @ev[]  B
  162. (NTH '(A B) 2)  @^@r[has an undefined effect]
  163. (NTH '() @i[n])@\@r[has an undefined effect for any @i[n]]
  164. @tabclear
  165. @end[ProgramExample]
  166. @EndDesc[NTH]
  167.  
  168. @info[NOTES="Settable"]
  169. @desc[(NTHCDR @i[list n]) @yl[] @i[list]]
  170. Returns the @i[n]@+[th] tail of @i[list].  In general, @i[list]
  171. must have at least @i[n] tails.
  172. @begin[ProgramExample]
  173. @tabclear
  174. (NTHCDR '(A B) 0)  @ev[]  (A B)
  175. (NTHCDR '(A B) 1)  @ev[]  (B)
  176. (NTHCDR '(A B) 2)  @ev[]  ()
  177. (NTHCDR '(A B) 3)  @^@r[has an undefined effect]
  178. (NTHCDR '() @i[n])@\@r[has an undefined effect for any nonzero @i[n]]
  179. @tabclear
  180. @end[ProgramExample]
  181.     @BeginInset[Bug:]
  182.     @tc[NTHCDR] doesn't handle @tc[SETTER] (i.e. is not settable) in
  183.     @Timp[] 2.7.
  184.     @EndInset[]
  185. @EndDesc[NTHCDR]
  186.  
  187. @info[NOTES="Settable"]
  188. @desc[@el[](LAST @i[list]) @yl[] @i[object]]
  189. Returns the last element of @i[list].
  190. @begin[ProgramExample]
  191. (LAST '(A B C))  @ev[]  C
  192. (LAST @i[list])  @ce[]  (CAR (LASTCDR @i[list]))
  193. @end[ProgramExample]
  194. @EndDesc[LAST]
  195.  
  196. @info[NOTES="Settable"]
  197. @desc[(LASTCDR @i[list]) @yl[] @i[pair]]
  198. Returns the last pair in @i[list].
  199. @begin[ProgramExample]
  200. (LASTCDR '(A B C))    @ev[]  (C)
  201. (LASTCDR '(A B . C))  @ev[]  (B . C)
  202. @end[ProgramExample]
  203.     @BeginInset[Bug:]
  204.     @tc[LASTCDR] doesn't handle @tc[SETTER] (i.e. is not settable) in
  205.     @Timp[] 2.7.
  206.     @EndInset[]
  207. @EndDesc[LASTCDR]
  208.  
  209.  
  210. @section[Lists as sequences]
  211.  
  212. @desc[@el[](LENGTH @i[list]) @yl[] @i[integer]]
  213. Return the length of @i[list].
  214. @begin[ProgramExample]
  215. (LENGTH '())       @ev[]  0
  216. (LENGTH '(A B C))  @ev[]  3
  217. @end[ProgramExample]
  218. @EndDesc[LENGTH]
  219.  
  220. @desc[@el[](APPEND . @i[lists]) @yl[] @i[list]]
  221. Constructs a list which is the concatenation of @i[lists].
  222. A new list is constructed, except that the result has
  223. the last non-null argument as a tail.
  224. @begin[ProgramExample]
  225. (APPEND '(A B C) '(D E) '(F G))  @ev[]  (A B C D E F G)
  226. @end[ProgramExample]
  227. @EndDesc[APPEND]
  228.  
  229. @info[EQUIV="NCONC"]
  230. @desc[(APPEND! . @i[lists]) @yl[] @i[list]]
  231. Destructive version of @tc[APPEND].  Splices the lists together,
  232. setting the cdr of the last pair in the first list to
  233. the second list, and so on.  Returns its first non-null argument.
  234.   @begin[ProgramExample]
  235. (DEFINE L1 (LIST 'A 'B 'C))
  236. (DEFINE L2 (LIST 'D 'E))
  237. L1                @ev[]  (A B C)
  238. (APPEND! L1 L2)   @ev[]  (A B C D E)
  239. L1                @ev[]  (A B C D E)
  240. (APPEND! '() L2)  @ev[]  (D E)
  241.   @end[ProgramExample]
  242. @EndDesc[APPEND!]
  243.  
  244. @desc[@el[](REVERSE @i[list]) @yl[] @i[list]]
  245. Returns a new list whose elements are those of @i[list], in reverse order.
  246.   @begin[ProgramExample]
  247. (REVERSE '(A B C))  @ev[]  (C B A)
  248.   @end[ProgramExample]
  249. @EndDesc[REVERSE]
  250.  
  251. @info[EQUIV="NREVERSE, DREVERSE"]
  252. @desc[(REVERSE! @i[list]) @yl[] @i[list]]
  253. This is a @qu"destructive" version of @tc[REVERSE];
  254. it allocates no storage for the result, but rather recycles
  255. the pairs in the source @i[list]'s to form the result list.
  256. @index[destructive]
  257. @EndDesc[REVERSE!]
  258.  
  259. @desc[(SUBLIST @i[list] @i[start] @i[count]) @yl[] @i[list]]
  260. Returns a list of @i[count] elements of @i[list],
  261. beginning with element @i[start].
  262.   @begin[ProgramExample]
  263. (SUBLIST '(A B C D E F) 2 3)  @ev[]  (C D E)
  264.   @end[ProgramExample]
  265. @enddesc[SUBLIST]
  266.  
  267.  
  268.  
  269. @section[Lists as sets]
  270.  
  271. @desc[(MEMQ? @i[object list]) @yl[] @i[boolean]]
  272. Returns true if @i[object] is an element of @i[list].
  273. @begin[ProgramExample]
  274. (MEMQ? 'B '(A B C))           @ev[]  @r[true]
  275. (MEMQ? 'B '(A (B C) D))       @ev[]  @r[false]
  276. (MEMQ? 'B '(B))               @ev[]  @r[true]
  277. (MEMQ? 'B '())                @ev[]  @r[false]
  278. (MEMQ? 17 '(10 17))           @ev[]  @r[undefined]
  279. (MEMQ? "foo" '("foo" "bar"))  @ev[]  @r[undefined]
  280. (LET ((X "foo"))
  281.   (MEMQ? X (LIST 'B X)))      @ev[]  @r[true]
  282. @end[ProgramExample]
  283. @EndDesc[MEMQ?]
  284.  
  285. @desc[@el[](MEM? @i[predicate object list]) @yl[] @i[boolean]]
  286. Returns true if @i[list] contains an object which
  287. is equal to @i[object] according to @i[predicate],
  288. which should be an equality predicate.
  289. @begin[ProgramExample]
  290. (MEM? = 17 '(10 17))                       @ev[]  @r[true]
  291. (MEM? STRING-EQUAL? "foo" '("foo" "bar"))  @ev[]  @r[true]
  292. @end[ProgramExample]
  293. @EndDesc[MEM?]
  294.  
  295. @desc[(ANY? @i[predicate] . @i[lists]) @yl[] @i[boolean]]
  296. Returns true if any element of @i[list] answers true to @i[predicate].
  297. @begin[ProgramExample]
  298. (ANY? NUMBER? '(FOO 15 BAR))  @ev[]  @r[true]
  299. @end[ProgramExample]
  300. @EndDesc[ANY?]
  301.  
  302. @desc[(EVERY? @i[predicate] . @i[lists]) @yl[] @i[boolean]]
  303. Returns true if every element of @i[list] answers true to @i[predicate].
  304. @begin[ProgramExample]
  305. (EVERY? SYMBOL? '(A B C))  @ev[]  @r[true]
  306. @end[ProgramExample]
  307. @EndDesc[EVERY?]
  308.  
  309. @desc[(DELQ @i[object list]) @yl[] @i[list]]
  310. Returns a list which is the same as the argument @i[list]
  311. with all occurrences of @i[object] removed.
  312. @EndDesc[DELQ]
  313.  
  314. @desc[@el[](DEL @i[predicate object list]) @yl[] @i[list]]
  315. Returns a list which is the same as @i[list] except that all
  316. elements which, according to @i[predicate],
  317. are equal to @i[object], have been removed.  
  318. @i[Predicate] should be an equality predicate.
  319. @ProgramExample[(DELQ @i[object list])  @ce[]  (DEL EQ? @i[object list])]
  320. @EndDesc[DEL]
  321.  
  322. @info[EQUIV="DELQ"]
  323. @desc[(DELQ! @i[object list]) @yl[] @i[list]]
  324. Destructive version of @tc[DELQ].
  325. Removes all occurrences of @i[object] from @i[list],
  326. possibly altering it, and returns the new and/or modified list.
  327. @EndDesc[DELQ!]
  328.  
  329. @desc[(DEL! @i[predicate object list]) @yl[] @i[list]]
  330. Destructive version of @tc[DEL].
  331. @ProgramExample[(DELQ! @i[object list])  @ce[]  (DEL! EQ? @i[object list])]
  332. @EndDesc[DEL!]
  333.  
  334.  
  335. @section[Mapping Procedures]
  336.  
  337. @info[EQUIV="MAPCAR"]
  338. @desc[@el[](MAP @i[proc] . @i[lists]) @yl[] @i[list]]
  339. Returns a list of the results of applying @i[proc] to successive elements
  340. of the @i[lists]; the @i[n]@+[th] element of the result list is the result
  341. of calling @i[proc] on the @i[n]@+[th] elements of the @i[lists].
  342. The length of the result list is the same as the length of the shortest
  343. of the @i[lists].
  344.   @begin[ProgramExample]
  345. (MAP (LAMBDA (X) (LIST 'A X)) '(CAT DOG))  @ev[]  ((A CAT) (A DOG))
  346. (MAP + '(10 20) '(5 6))                    @ev[]  (15 26)
  347. (MAP LIST '(A B) '(C D E) '(F))            @ev[]  ((A C F))
  348.   @end[ProgramExample]
  349. @EndDesc[MAP]
  350.  
  351. @info[EQUIV="MAPLIST"]
  352. @desc[(MAPCDR @i[proc] . @i[lists]) @yl[] @i[list]]
  353. @tc[MAPCDR] is similar to @tc[MAP], except that @i[proc] is applied to
  354. successive @i[tails] of the @i[lists].
  355. @EndDesc[MAPCDR]
  356.  
  357. @desc[(MAP! @i[proc] @i[list]) @yl[] @i[list]]
  358. For each pair in @i[list], applies @i[proc] to the car of the pair,
  359. and then modifies the pair so that its car is the value of the
  360. application.
  361.   @begin[ProgramExample]
  362. (DEFINE L (LIST 'CAT 'DOG))  @ev[]  (CAT DOG)
  363. (MAP! (LAMBDA (X) (LIST 'A X)) L)
  364. L  @ev[]  ((A CAT) (A DOG))
  365.   @end[ProgramExample]
  366. @EndDesc[MAP!]
  367.  
  368. @info[EQUIV="MAPC"]
  369. @desc[@el[](WALK @i[proc] . @i[lists]) @yl[] @i[undefined]]
  370. This is similar to @tc[MAP] but the result is @comment{the value of the last
  371. call to @i[proc] instead of a list of the result values.}
  372. a value of no particular interest.
  373. Thus @tc[WALK] is useful only for the side-effects that @i[proc] performs.
  374. @comment{not for the return value.}
  375. @EndDesc[WALK]
  376.  
  377. @info[EQUIV="MAP"]
  378. @desc[(WALKCDR @i[proc] . @i[lists]) @yl[] @i[undefined]]
  379. This is similar to @tc[MAPCDR] but the result is a value of no particular
  380. interest.
  381. @EndDesc[WALKCDR]
  382.  
  383.  
  384. @section[Lists as associations]
  385.  
  386. @desc[(ASSQ @i[object list]) @yl[] @i[pair] @r[or] @i[false]]
  387.   @begin[ProgramExample]
  388. (ASSQ @i[object list])  @ce[]  (ASS EQ? @i[object list])
  389.   @end[ProgramExample]
  390. @EndDesc[ASSQ]
  391.  
  392. @desc[@el[](ASS @i[predicate object list]) @yl[] @i[pair] @r[or] @i[false]]
  393. An @iix[association list]
  394. is a list of pairs that represents
  395. a @qu"lookup table" where the car of each pair matches a lookup key.  @tc[ASS]
  396. and its related procedures search association lists by comparing
  397. (applying @i[predicate]) the key (@i[object]) against the car of each
  398. element of @i[list] (i.e., the caar of each tail).  If it finds a match,
  399. it returns that element (the car of the tail).  If it finds no match,
  400. it returns false.
  401.   @begin[ProgramExample]
  402. (ASS EQ? 'B '((A 10 20) (B 30 40) (C 50 60)))  @ev[]  (B 30 40)
  403. (ASS = 9 '((1 Y) (4 Y) (9 Z) (12 P)))          @ev[]  (9 Z)
  404. (ASS = 10 '((1 Y) (4 Y) (9 Z) (12 P)))         @ev[]  @r[false]
  405.   @end[ProgramExample]
  406. @EndDesc[ASS]
  407.  
  408.  
  409. @section[Lists as stacks]
  410.  
  411. @tc[PUSH] and @tc[POP] permit a convenient syntax for the use of lists
  412. in implementing simple stacks.  They are assignment forms, like @tc[SET]
  413. and @tc[INCREMENT].  See section @ref[Assignmentsection] for a general
  414. discussion of assignment forms.
  415.  
  416. @info[NOTES="Special form"]
  417. @desc[(PUSH @i[location object]) @yl[] @i[undefined]]
  418. @dc{ Bogus description - fix. }
  419. Cons a pair whose car is @i[object] and whose cdr is 
  420. the value in @i[location] (usually a list), and
  421. store the extended list back into @i[location].
  422.   @begin[ProgramExample]
  423. (LSET L '(34 55))  @ev[]  (34 55)
  424. (PUSH L 21)
  425. L                  @ev[]  (21 34 55)
  426. (PUSH L '(A B C))
  427. (PUSH (CAR L) 'D)
  428. L                  @ev[]  ((D A B C) 21 34 55)
  429.   @end[ProgramExample]
  430.  
  431. In general,
  432.   @begin[ProgramExample]
  433. (PUSH @i[location object])
  434.   @ce[]  (SET @i[location] (CONS @i[object location]))
  435.   @ce[]  (MODIFY @i[location] (LAMBDA (L) (CONS @i[object] L)))
  436.   @end[ProgramExample]
  437. @EndDesc[PUSH]
  438.  
  439. @info[NOTES="Special form"]
  440. @desc[(POP @i[location]) @yl[] @i[object]]
  441. The car of the value in @i[location]
  442. (this value should be a pair) is returned.  As a side-effect,
  443. the cdr of the list is stored back in the location.
  444.   @begin[ProgramExample]
  445. L        @ev[]  (21 34 55)
  446. (POP L)  @ev[]  21
  447. L        @ev[]  (34 55)
  448.   @end[ProgramExample]
  449. In general,
  450.   @begin[ProgramExample]
  451. (POP @i[location])  @ce[]  @~
  452.   (BLOCK0 (CAR @i[location]) (SET @i[location] (CDR @i[location])))
  453.                     @ce[]  @~
  454.   (BLOCK0 (CAR @i[location]) (MODIFY @i[location] CDR))
  455.   @end[ProgramExample]
  456. @EndDesc[POP]
  457.